//
//  WYChannelView.m
//  WYNews
//
//  Created by 曾佳豪 on 2017/3/18.
//  Copyright © 2017年 haodada.com. All rights reserved.
//

#import "WYChannelView.h"
#import "WYLable.h"
const int baseTag =777;
@implementation WYChannelView


-(instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        
        self.backgroundColor =[UIColor whiteColor];
        //调用模型的类方法 获取模型数组
        _channelModelArr =[WYChannelModel channelModelArr];
    
        //调用设置UI方法 添加所有的频道L
        [self setupUI];
        
    }
    return  self;
    
}
-(void)setupUI{
    //隐藏滚动条
    self.showsHorizontalScrollIndicator =NO;
    //添加x的变量
    CGFloat origin =0;
    
    //label 的index
    NSInteger index = 0;
    //遍历模型数组 添加Label
    for ( WYChannelModel *model in _channelModelArr) {
        
        //用L的构造器创建label
        WYLable *label =[WYLable labelwithModel:model];
      //定义label的 frame
        CGRect frame =CGRectMake(origin, 0, label.bounds.size.width, 40);
        //赋值给label
        label.frame =frame;
        //每创建1个label origin(x)的值修改为下一个被创建的label的origin
        origin +=label.bounds.size.width;
        
        //Label默认是没选中状态
        label.isSelected =NO;
        
        //给label 设一个tag
        //(所有view的默认的tag是0, 添加baseTag防止有多个子视图tag都为0时, 取错对象)
        label.tag =baseTag+index;
        
        //打开用户交互
        label.userInteractionEnabled =YES;
        //给label添加点击事件
        UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changSelectIndex:)];
        [label addGestureRecognizer:tap];
        
        //添加label
        [self addSubview:label];
        //每循环一次 index要+1
        index +=1;
    }
    //默认将第0个label设为高亮
    self.selectedIndex = 0;
    
    //设置contenSize(滚动范围)
    self.contentSize =CGSizeMake(origin, 40);
}
//点击了Label之后切换频道
-(void)changSelectIndex:(UITapGestureRecognizer *)tap{
    
    //获取被点的label 和被点击的index
    WYLable *label =(WYLable *)tap.view;
    //用被点的label的tag -全局的tag = 当前选中的index
    NSInteger index =label.tag -baseTag;
    
    //设置当前选中的index  调用重写的setSelectedIndex 属性的set方法
    //当改变 selectedIndex 值的时候, 可以同时改变对应的label的选中状态
    [self setSelectedIndex:index];
    
    //让代理切换频道
    if (_channelDelegate !=nil && [_channelDelegate respondsToSelector:@selector(channelChanged:)]) {
        //响应了就传给代理对象index
        [_channelDelegate channelChanged:index];
    }
}
//对应的将选中label设置为选中 or 取消

//传进来的就是选中的index
-(void)setSelectedIndex:(NSInteger)selectedIndex{
    //把老的取消选中状态
    //对应的label应该取消选中状态(默认没赋值的为取消)
    WYLable *selectedLabel =[self viewWithTag:_selectedIndex+baseTag];
    selectedLabel.isSelected =NO;
    
    //给新的设置为选中状态
    //selectedIndex //对应的label应该设为选中状态
    WYLable *shouldSelectedLabel =[self viewWithTag:selectedIndex +baseTag];
    shouldSelectedLabel.isSelected =YES;
    
    //解决 点击的label超过了屏幕的bug
    //先拿到选中的label的 x (往左的)和 Label的x (往右)
    CGFloat originX =shouldSelectedLabel.frame.origin.x;
    CGFloat rightX  =shouldSelectedLabel .frame.origin.x+shouldSelectedLabel.bounds.size.width;
    //判断 x <滚动的范围.x || 右边x >滚动的范围.x
    if (originX <self.contentOffset.x ||rightX >self.contentOffset.x) {
         //成立就 将label 滚动到可见区域
        [self scrollRectToVisible:shouldSelectedLabel.frame animated:YES];
    }
    
    //最后新的也成了老的了 也就是选中状态
    _selectedIndex =selectedIndex;
    
    
}
@end
